package cc.rengu.igas.opms.core.service.trans.quickpay;

import cc.rengu.igas.opms.common.enums.RespCodeEnum;
import cc.rengu.igas.opms.common.enums.SignTypeEnum;
import cc.rengu.igas.opms.core.service.base.ManageService;
import cc.rengu.igas.opms.facade.request.SignAcctInfoQueryRequest;
import cc.rengu.igas.opms.facade.response.SignAcctInfoQueryResponse;
import cc.rengu.igas.share.core.realize.MchntService;
import cc.rengu.igas.share.core.realize.impl.MchntServiceImpl;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.SignAcctInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.SignAcctInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.SignAcctInfo;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 银行卡签约信息查询
 * <p>
 * Created by liujinan on 2020/4/15.
 * modify by wangcheng 2021-09-13
 */

public class SignAcctInfoQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        SignAcctInfoQueryRequest signAcctInfoQueryRequest = new SignAcctInfoQueryRequest();
        ConvertUtil.convertOutput(signAcctInfoQueryRequest);
        return signAcctInfoQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {

        SignAcctInfoQueryRequest signAcctInfoQueryRequest = (SignAcctInfoQueryRequest) request;
        String instId = signAcctInfoQueryRequest.getHeader().getInstId();
        String platMchntNo = signAcctInfoQueryRequest.getPlatMchntNo();

        //step1.用户主体，签约主体，签约类型赋值
        String signInst = instId;
        String signType;
        if (null == platMchntInfo) {
            /* 判断商户号是否为普通商户 */
            MchntService mchntService = new MchntServiceImpl();
            mchntInfo = mchntService.getMchntInfo(instId, platMchntNo);
            if (null != mchntInfo) {
                signType = mchntInfo.getMchntSignType();
                if (SignTypeEnum.PLAT_CLASS_SIGN.getSignTypeCode().equals(signType)) {
                    signInst = mchntInfo.getPlatMchntNo();
                }
            } else {
                rglog.error("商户信息不存在!");
                throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
            }
        } else {
            signType = platMchntInfo.getSignType();
            if (SignTypeEnum.MCHNT_CLASS_SIGN.getSignTypeCode().equals(signType)) {
                signInst = platMchntInfo.getPlatMchntNo();
            } else if (SignTypeEnum.PLAT_CLASS_SIGN.getSignTypeCode().equals(signType)) {
                signInst = platMchntInfo.getPlatMchntNo();
            }
        }

        //获取签约信息
        rglog.debug("获取签约信息...");
        SignAcctInfoMapper signAcctInfoMapper = new SignAcctInfoMapperImpl();
        List<SignAcctInfo> signAcctInfos = signAcctInfoMapper.selectSignAcctInfoBySignAcctNo(instId, signAcctInfoQueryRequest.getPayAcctNo());

        //判断签约状态
        if (null == signAcctInfos || signAcctInfos.isEmpty()) {
            //签约信息不存在
            rglog.info("签约信息不存在");
            throw new BizException(RespCodeEnum.GET_SIGN_INFO_ERROR.getRespCode(), RespCodeEnum.GET_SIGN_INFO_ERROR.getRespDesc());
        } else {
            String finalSignInst = signInst;
            signAcctInfos = signAcctInfos.stream().filter(item -> item.getSignInst().equals(finalSignInst) &&
                    item.getSignStatus().equals(AppParamConstant.YES)).collect(Collectors.toList());
        }

        SignAcctInfo signAcctInfo = signAcctInfos.get(0);

        //判断证件号后六位是否相等
        if (!signAcctInfo.getCertNo().endsWith(signAcctInfoQueryRequest.getCertNo())) {
            rglog.info("证件号码不一致，requestCertNo:<{}>,signCertNo:<{}>", signAcctInfoQueryRequest.getCertNo(), signAcctInfo.getCertNo());
            throw new BizException(RespCodeEnum.CERT_NO_DIFF.getRespCode(), RespCodeEnum.CERT_NO_DIFF.getRespDesc());
        }

        //组响应信息
        SignAcctInfoQueryResponse signAcctInfoQueryResponse = new SignAcctInfoQueryResponse();
        signAcctInfoQueryResponse.setSignPhoneNo(signAcctInfo.getReserveMobileNo());
        signAcctInfoQueryResponse.setSignTokenType(signAcctInfo.getSignTokenType());
        signAcctInfoQueryResponse.setSignAcctType(signAcctInfo.getSignAcctType());
        signAcctInfoQueryResponse.setSignBankNo(signAcctInfo.getSignBankNo());
        signAcctInfoQueryResponse.setSignBankName(signAcctInfo.getSignBankName());
        BizResponse<SignAcctInfoQueryResponse> bizResponse = new BizResponse();
        bizResponse.setResult(signAcctInfoQueryResponse);
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        SignAcctInfoQueryResponse signAcctInfoQueryResponse = (SignAcctInfoQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(signAcctInfoQueryResponse);
    }
}
